<?php
/**
 * Created by PhpStorm.
 * User: zhangyi
 * Date: 17/3/10
 * Time: 下午9:54
 */
namespace backend\controllers;

use app\models\HeaderInfo;
use app\models\SmsLog;
use yii\web\Controller;
use Yii;
use backend\components\Sms;

class BaseInterfaceController extends Controller
{
    public $enableCsrfValidation = false;
    protected $status = 1;
    protected $message = '操作成功';
    protected $content;
    protected $originData;//请求源数据

    public function init()
    {
        parent::init();
        $this->originData = file_get_contents("php://input");
	    file_put_contents(Yii::getAlias('@webroot/request_log.txt'),$this->originData."\n",FILE_APPEND);

        $this->originData = json_decode($this->originData,true);

        if($this->encryption() === false ) {
            $this->status=0;
            $this->message='token校验失败';
            $this->outputJson();
        }
    }
    protected function outputJson()
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        Yii::$app->response->data = array(
            'header'=>array(
                'status'=>$this->status,
                'message'=>$this->message,
            ),
            'content'=>$this->content,
        );
        Yii::$app->end();
    }
    //加密算法
    private function encryption()
    {
        return 111;
        $post = $this->originData;

        unset($post['deviceToken']);
        ksort($post);
        $hash = hash('sha512',json_encode($post));
        $hash = substr($hash,0,64);
        $hashArray = str_split($hash);
        $result = '';
        foreach($hashArray as $index=>$value) {
            if($index%2 == 1) {
                $result .= $value;
            }
        }
        return $result === $this->originData['deviceToken'];
    }
    //检查手机号码有效性
    protected function verifyMobileValid($mobile)
    {
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }
    //给手机号发送验证码
    protected function sms_send($mobile,$message)
    {
        Sms::send($mobile,$message);
        return true;
    }
    //检查手机号申请发送验证码短信是否达到每日限制
    protected function checkSmsSendLimit($mobile)
    {
//        return true;
        $start = strtotime(date('Y-m-d'));
        $end = $start+24*3600;
        $query = SmsLog::find()
            ->where(['>=','create_time',$start])
            ->andWhere(['<','create_time',$end]);

        if($query->andWhere(['mobile'=>$mobile])->count() >5 ) {    //相同手机号每天不超过5个
            return false;
        }
        if($query->andWhere(['ip'=>$_SERVER['REMOTE_ADDR']])->count() > 20 ) {  //相同ip每天不超过20个
            return false;
        }
        return true;
    }

    //记录用户注册和登录时的头信息(包括设备ID/软件版本等信息)
    protected function logHeaderInfo($uid,$type)
    {
        $model = new HeaderInfo();
        $model->uid = $uid;
        $model->type = $type;
        $model->app_id = intval($this->originData['app_id']);
        $model->device_id = $this->originData['device_id'];
        $model->app_build_code = $this->originData['app_build_code'];
        $model->app_version = $this->originData['app_version'];
        $model->deviceInfo = $this->originData['deviceInfo'];
        $model->channel = $this->originData['channel'];
        $model->product = $this->originData['product'];
        return $model->save();
    }
    //返回htmlspecialchars编码
    protected function safe($str)
    {
        //return str_replace(array('%','&'),array('％','＆'),$str);
        return htmlspecialchars_decode(str_replace(array('%','&nbsp;'),array('％',' '),$str),ENT_QUOTES);
//        return htmlspecialchars($str,ENT_QUOTES);
    }
}
